Page({
  data: {
    code:'',
    from:'',
    goodsId:''
  },
  onLoad(option) {
    console.log(option,'0705')
    var from = '';
    if(option){
     from = option.from
    }
    if(from=='productDetail'){
      this.setData({
        goodsId:option.goodsId
       })
       console.log(option.goodsId, this.data.goodsId)
    }
    // var from = option.from?option.from:'member'
    
   this.setData({
    from:from
   })
    var that = this;
    wx.login({
      success (res) {
        // console.log(res)
        if (res.code) {
          that.setData({
            code:res.code,
          })
          wx.setStorage({
            key: "code",
            data:res.code
          })
         
        } else {
          wx.showToast({
            // title: res.data.datas.message,
            title: "登录失败",
            icon: 'error',
            duration: 2000
          })
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
    
  },
  getPhoneNumber(e){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    console.log("code:",that.data.code,encodeURIComponent(e.detail.iv),encodeURIComponent(e.detail.encryptedData))
    //发起网络请求
    wx.request({
      method:'POST',
      url:`${getApp().globalData.webUrl}/mobile/index.php?act=connect&op=applet_userlogin&code=${that.data.code}&iv=${encodeURIComponent(e.detail.iv)}&encryptedData=${ encodeURIComponent(e.detail.encryptedData)}`,//自己的服务器的请求地址接口
     
      success (res) {
        wx.hideLoading()
        console.log(res.data.datas)
        if(res.data.datas.code==200){
         
          // console.log(res.data)
          var getDatas = res.data.datas
          var storageData ={
            hasLogin:true,
            userInfo:{
              key: res.data.datas.key, 
              username: res.data.datas.username, 
              userid: res.data.datas.userid, 
              openid: res.data.datas.openid,
              unionid: res.data.datas.unionid
            }
          } 
          wx.setStorage({
            key: "loginInfo",
            data:storageData
          })
         
          getApp().globalData.hasLogin=true;
          getApp().globalData.userInfo=storageData;
          wx.showToast({
            // title: res.data.datas.message,
            title: "登录成功",
            icon: 'success',
            duration: 1000,
            success:function(){
              if(that.data.from=='cartList'){
                wx.reLaunch({ url: `../index/index?type=cartList` })
               // wx.switchTab({ url: `../cartlist/cartlist` });
              }else if(that.data.from=='productDetail'){
                console.log(that.data.goodsId)
                wx.reLaunch({ url: `../index/index?type=productDetail&goodsId=${that.data.goodsId}` })
               // wx.switchTab({ url: `../cartlist/cartlist` });
              }else if(that.data.from=='member'){
                wx.reLaunch({ url: `../index/index?type=member` })
               // wx.switchTab({ url: `../cartlist/cartlist` });
              }else{
                wx.reLaunch({ url: `../index/index?type=index` })
               // wx.switchTab({ url: `../home/home` });
              }
            }
          })
         
          
        }else{
          wx.showToast({
            // title: res.data.datas.message,
            title: "登录失败",
            icon: 'error',
            duration: 2000
          })
          that.onLoad();
        }
      },
      fail (res) {
       console.log(res)
      },
    })   
  },
  
 
})